package com.iceberg;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseAdapter;
import java.util.Arrays;

public class AlgorithmVisualizer {

    // TODO: 创建自己的数据
    private int[] money;        // 数据
    private AlgorithmFrame frame;    // 视图
    private int DELAY = 40;

    public AlgorithmVisualizer(int sceneWidth, int sceneHeight){

        // 初始化数据
        // TODO: 初始化数据
        money = new int[100];
        for (int i = 0; i < money.length; i ++){
            money[i] = 100;
        }

        // 初始化视图
        EventQueue.invokeLater(() -> {
            frame = new AlgorithmFrame("Welcome", sceneWidth, sceneHeight);
            // TODO: 根据情况决定是否加入键盘鼠标事件监听器
            frame.addKeyListener(new AlgoKeyListener());
            frame.addMouseListener(new AlgoMouseListener());
            new Thread(() -> {
                run();
            }).start();
        });
    }

    // 动画逻辑
    private void run(){

        // TODO: 编写自己的动画逻辑
        while (true){
            Arrays.sort(money);
            frame.render(money);
            AlgorithmVisHelper.pause(DELAY);
            for (int k = 0; k < 50; k++){
                for (int i =0; i< money.length; i++){
                    int j = (int)(Math.random() * money.length);
                    money[i] = money[i] - 1;
                    money[j] = money[j] + 1;
                }
            }
        }
    }

    // TODO: 根据情况决定是否实现键盘鼠标等交互事件监听器类
    private class AlgoKeyListener extends KeyAdapter{ }
    private class AlgoMouseListener extends MouseAdapter{ }

    public static void main(String[] args) {

        int sceneWidth = 800;
        int sceneHeight = 800;

        // TODO: 根据需要设置其他参数，初始化visualizer
        AlgorithmVisualizer visualizer = new AlgorithmVisualizer(sceneWidth, sceneHeight);
    }
}
